from google.appengine.api import users
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app, login_required
from models import *
import logging

class HomePage(webapp.RequestHandler):
  @login_required
  def get(self):
    user = users.get_current_user()
    coordinates = Experiment.all().filter('coordinator =', user)
    collaborates = Experiment.all().filter('collaborators = ', user)
    watches = Experiment.all().filter('watchers = ', user)
    self.response.out.write(template.render('templates/home.html', {
      'user': user,
      'coordinates': coordinates,
      'collaborates': collaborates,
      'watches': watches
    }))

class Create(webapp.RequestHandler):
  @login_required
  def get(self):
    values = {
      'form' : CreateExperimentForm()
    }
    self.response.out.write(template.render('templates/create.html', values))
  def post(self):
    user = users.get_current_user()
    if not user:
      self.redirect(users.create_login_url(self.request.uri))
    else:
      form = CreateExperimentForm(data=self.request.POST)
      if form.is_valid():
        entity = form.save(commit=False)
        entity.coordinator = users.get_current_user()
        entity.put()
        scenario = Scenario()
        scenario.experiment = entity
        scenario.put()
        self.redirect('/display?'+str(entity.key()))
      else:
        self.response.out.write(template.render('templates/create.html', {'form': form}))

varname = lambda value: value.split('|')[0]
unit = lambda value: value.split('|')[1]

class Display(webapp.RequestHandler):
  @login_required
  def get(self):
    experiment = db.get(self.request.query_string)
    scenarios = list(Scenario.all().filter('experiment = ', experiment))
    for s in scenarios:
      s.steps = list(Step.all().filter('scenario = ', s))
      logging.error(s.steps)
    values = {
      'experiment': experiment,
      'scenarios': scenarios,
      'hypothesis': EditHypothesisForm(),
      'step_form': AddStepForm()
    }
    self.response.out.write(template.render('templates/display.html', values))

class AddVar(webapp.RequestHandler):
  @login_required
  def get(self):
    scenario = db.get(self.request.query_string)
    values = { 'form': AddVariableForm() }
    self.response.out.write(template.render('templates/addvar.html', values))
  def post(self):
    scenario = db.get(self.request.POST['scenario'])
    scenario.variables += [self.request.POST['var']]
    scenario.put()
    self.redirect('/display?'+str(scenario.experiment.key()))

class AddStep(webapp.RequestHandler):
  @login_required
  def get(self):
    scenario = db.get(self.request.query_string)
    values = { 'form': AddStepForm(), 'scenario': scenario }
    self.response.out.write(template.render('templates/addstep.html', values))
  def post(self):
    scenario = db.get(self.request.POST['scenario'])
    steps = Step.all().filter('scenario = ', scenario)
    form = AddStepForm(data=self.request.POST)
    new_step = form.save(commit=False)
    new_step.scenario = scenario
    new_step.variables = self.request.get_all('variables')
    new_step.put()
    self.redirect('/display?'+str(scenario.experiment.key()))

application = webapp.WSGIApplication([('/', HomePage), 
  ('/create', Create), 
  ('/display', Display),
  ('/addvar', AddVar),
  ('/addstep', AddStep)], debug = True)

if __name__ == '__main__':
  run_wsgi_app(application)
